//
//  MDProbCls.m
//  evidenceGather
//
//  Created by 王继伟 on 13-8-21.
//  Copyright (c) 2013年 无. All rights reserved.
//

#import "MDProbCls.h"
#import "ProblemClassVO.h"
#import "MDProbCls.h"
#import "MDProject.h"
 

@implementation MDProbCls
 
//数据库插入记录对象
-(NSString *)insertProjClsVO:(ProblemClassVO *) projclsVO{
    NSString *insertSQL=[[NSString alloc] initWithFormat:@"insert into problemClass(pccode,pcname,loginuser) values('%@','%@','%@');",projclsVO.pccode,projclsVO.pcname,projclsVO.loginuser];
    //NSLog(@"the insert sql is %@",insertSQL);
    [super.dbDelegate executeUpdate:insertSQL];
    return projclsVO.pccode;
}


-(void *)updateProjClsVO:(ProblemClassVO *) projclsVO{
    NSString *insertSQL=[[NSString alloc] initWithFormat:@"update problemClass set pcname='%@',loginuser='%@' where pccode='%@'" ,projclsVO.pcname,projclsVO.loginuser,projclsVO.pccode];
    //NSLog(@"the insert sql is %@",insertSQL);
    [super.dbDelegate executeUpdate:insertSQL]; 
}


//新增或者更新问题分类信息
-(void )update:(NSArray *) projclsVOs{
    if(projclsVOs!=nil){
        int count=[projclsVOs count];
        for (int i=0;i<count;i++) {
           ProblemClassVO *probClsVO= [projclsVOs objectAtIndex:i];
           ProblemClassVO *tempClsVO= [self qryProClsByPk:probClsVO.pccode];
            //如果不存在，则新增
            if(tempClsVO==nil){
                [self insertProjClsVO:probClsVO];
            }else{
                [self updateProjClsVO:probClsVO];
            }
        }
        
    }
    
}

//根据用户查询项目类别
-(ProblemClassVO *)qryProClsByPk:(NSString *) pccode{
    NSString *sqlWhere=@" pccode='";
    sqlWhere=[sqlWhere stringByAppendingString:pccode];
    sqlWhere=[sqlWhere stringByAppendingString:@"'"];
    NSMutableDictionary *dic=[self qryProjclsBySqlWhere:sqlWhere];
    if(dic!=nil){
        return [dic objectForKey:pccode];
    }
    return nil;
    
}


//根据用户查询项目类别
-(NSMutableDictionary *)qryProClsByPkUser:(NSString *) userName{
    NSString *sqlWhere=@" loginuser='";
    sqlWhere=[sqlWhere stringByAppendingString:userName];
    sqlWhere=[sqlWhere stringByAppendingString:@"'"];
    return [self qryProjclsBySqlWhere:sqlWhere];

}

//根据条件查询项目类别
-(NSMutableDictionary *)qryProjclsBySqlWhere:(NSString *) sqlWhere{
    NSMutableDictionary *result = nil;
    if(nil != sqlWhere && [sqlWhere length] > 0)
    {
        NSMutableString *sql = [[NSMutableString alloc]initWithString:@"select pccode,pcname,loginuser from problemClass where"];
        [sql appendString:sqlWhere];
        //执行查询语句
        NSArray *rs=[super.dbDelegate executeQuery:sql];
        if(nil != rs && [rs count] > 0){
            //如果查询结果不为空，则初始化dictionary，并循环赋值
            result = [[NSMutableDictionary alloc]init];
            for(int i=0;i < [rs count]; i++){
                NSDictionary *dic = [rs objectAtIndex:i];
                //获取附件对应的主键
                NSString *key = [[NSString alloc] initWithData:[dic objectForKey:@"pccode"] encoding:NSUTF8StringEncoding];
                ProblemClassVO *projclsVO = [result objectForKey:key];
                if(nil == projclsVO)   //当结果集内无对应的Bug记录时
                {
                    projclsVO = [[ProblemClassVO alloc]init];
                    projclsVO.pccode = key;
                    projclsVO.pcname =[[NSString alloc] initWithData:[dic objectForKey:@"pcname"] encoding:NSUTF8StringEncoding];
                    projclsVO.loginuser = [[NSString alloc] initWithData:[dic objectForKey:@"loginName"] encoding:NSUTF8StringEncoding];
                    [result setObject:projclsVO forKey:key];
                }
            }
        }
    }
    return result;
    
}
//根据用户查询项目类别
-(NSMutableDictionary *)qryAllProCls{
    NSString *sqlWhere=@" 1=1%"; 
    return [self qryProjclsBySqlWhere:sqlWhere];
}
@end
